import {
  tokenCheckReq,
  loadEmpInfoReq,
  loadPermissionReq
} from "@/services/auth";
import store from "@/store";
export function verify_home() {
  return new Promise(function(resolve, reject) {
    const token = uni.getStorageSync("token");
    if (token) {
      // 验证token是否过期
      tokenCheckReq(token)
        .then(res => {
          store.commit("register/updateToken", token);
          // uni.showLoading({
          //   title: "数据加载中"
          // });
          // 根据token查询员工用户相关信息
          loadEmpInfoReq()
            .then(res => {
              store.commit("user/updateUserInfo");
              store.commit("site/updateCompanyInfo");
              resolve();
              uni.hideLoading();
              if (!!res) {
                uni.setStorageSync("siteId", res.siteId);
                uni.setStorageSync("idCard", res.idCard);
                uni.setStorageSync("idName", res.name);
                uni.setStorageSync("employeeId", res.id);
                uni.setStorageSync("employeeName", res.name);
                uni.setStorageSync("companyId", res.companyId);
                uni.setStorageSync("companyName", res.companyName);
                uni.setStorageSync("verified", res.verified);
                uni.setStorageSync("wgkInfo", res.vxWgkUserAuthResultVO);
                uni.setStorageSync("wgk_verified", res.vxWgkUserAuthResultVO && res.vxWgkUserAuthResultVO.authorize_state === "AUTHORIZED");
                uni.setStorageSync("userStatus", res.status);
                store.commit("user/updateUserInfo");
                store.commit("site/updateCompanyInfo");
                store.dispatch("common/loadAreasAct");
                store.dispatch("common/loadEmployeesAct");
                store.dispatch("common/loadAdminsAct");
                store.dispatch("common/loadRolesAct");
                store.dispatch("common/loadPositionsAct");
                store.dispatch("common/loadDepartmentsAct");
              } else {
                store.commit("site/clearSiteInfo");
                store.commit("user/clearUserInfo");
              }
              // 获取租户信息
              loadPermissionReq()
                .then(res => {
                  // console.log("res", res);
                  store.commit("site/updateSiteInfo", res.siteList);
                })
                .catch(err => {
                  reject(err);
                  console.log("debug log --> ", err);
                });
            })
            .catch(err => {
              reject(err);
              uni.hideLoading();
              console.log("debug log --> ", err);
              uni.showToast({
                title: err,
                icon: "none",
                duration: 2000
              });
            });
        })
        .catch(err => {
          reject(err);
          console.log("debug log --> ", err);
          uni.showToast({
            title: err,
            icon: "none",
            duration: 2000
          });
        });
    } else {
      reject();
    }
  });
}

export function verify_user() {
  return new Promise(function(resolve, reject) {
    const token = uni.getStorageSync("token");
    // 判断是否有token
    if (!!token) {
      uni.navigateTo({
        url: "/authorize/index"
      });
    }
  });
}

export function verify_token() {
  return new Promise(function(resolve, reject) {
    const token = uni.getStorageSync("token");
    // 判断是否有token
    if (token) {
      // 验证token是否过期
      tokenCheckReq(token)
        .then(res => resolve())
        .catch(() => reject());
    } else {
      reject();
    }
  });
}
